package com.taotao.st3.base.jackson.localdatetime;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


/**
 * 将 yyyy-MM-dd 转换为 yyyy-MM-dd 23:59:59  LocalDateTime
 */
public class EndOfDayLocalDateTimeDeserializer extends LocalDateTimeDeserializer {
    private static final long serialVersionUID = 1L;
    private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE_TIME;

    private EndOfDayLocalDateTimeDeserializer() {
        super(DEFAULT_FORMATTER);
    }

    public EndOfDayLocalDateTimeDeserializer(DateTimeFormatter formatter) {
        super(formatter);
    }

    @Override
    protected LocalDateTimeDeserializer withDateFormat(DateTimeFormatter formatter) {
        return new EndOfDayLocalDateTimeDeserializer(formatter);
    }

    @Override
    public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        LocalDate localDate = new LocalDateDeserializer(_formatter).deserialize(p, ctxt);
        LocalDateTime localDateTime = localDate.atTime(23, 59, 59, 999999999);
        return localDateTime;
    }
}